<template>
	<div class="app-container" style="background: #fff;">
		<div ref="box" class="flex-between-center margin-bottom-4">
			<div class="flex-start-center">
				<el-dropdown trigger="click" class="item-gap" size="mini" @command="onArrowLeft">
					<el-button type="primary" icon="el-icon-arrow-left" size="mini">{{ searchParams.dateFrom }}</el-button>
					<el-dropdown-menu slot="dropdown">
						<el-dropdown-item command="1">前移一天</el-dropdown-item>
						<el-dropdown-item command="7">前移一周</el-dropdown-item>
					</el-dropdown-menu>
				</el-dropdown>
				<div>
					<el-button class="item-gap" type="primary" size="mini" @click="onArrowNow">今天</el-button>
				</div>
				<el-dropdown trigger="click" size="mini" @command="onArrowRight">
					<el-button type="primary" size="mini">
						<span>{{ searchParams.dateTo }}</span>
						<span class="el-icon-arrow-right"></span>
					</el-button>
					<el-dropdown-menu slot="dropdown">
						<el-dropdown-item command="1">后移一天</el-dropdown-item>
						<el-dropdown-item command="7">后移一周</el-dropdown-item>
					</el-dropdown-menu>
				</el-dropdown>
			</div>
			<div class="flex-end-center">
				<el-radio-group v-model="tableHeaderSwitchType" size="mini" class="item-gap">
					<el-radio-button label="date">日</el-radio-button>
					<el-radio-button label="week">周</el-radio-button>
				</el-radio-group>
				<el-select v-model="searchParams.workshopId" clearable placeholder="区域" size="mini" class="search-bar-item-size1 item-gap" @clear="onSearchBtn" @change="onSearchBtn">
					<el-option v-for="item in workshopList" :key="item.id" :label="item.name" :value="item.id" />
				</el-select>
				<el-select v-model="searchParams.processId" clearable placeholder="工序" size="mini" class="search-bar-item-size1 item-gap" @clear="onSearchBtn" @change="onSearchBtn">
					<el-option v-for="item in processList" :key="item.id" :label="item.name" :value="item.id" />
				</el-select>
				<jk-search-input v-model="searchParams.nameOrCode" size="mini" show-search placeholder="设备编号或名称" class="search-bar-item-size3" @on-search="onSearchBtn" @change="onSearchBtn"></jk-search-input>
			</div>
		</div>
		<ganttChart
			:table-loading="tableLoading"
			:height="tableHeight + 20"
			:default-percent="20"
			:table-header-switch-type="tableHeaderSwitchType"
			:table-option="{
				leftTableHeader: tableHeader,
				dateFrom: searchParams.dateFrom,
				dateTo: searchParams.dateTo,
				rowList: tableData,
				startDateKeyName: 'subPlanStartDate',
				endDateKeyName: 'subPlanEndDate',
			}"
			@on-check-change="onCheckChange"
		></ganttChart>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="searchParams.count"
				location="dialog"
				@on-change="onPageChange"
			/>
		</footer>
	</div>
</template>

<script>
    import ganttChart from '../../jk-components/jk-gantt';
    import calHeight from '../../../mixins/cal-table-height';
    import { formatDate, pageConfig, tipsBar } from '../../../utils/jk/jk-tool';
    import { productionPrdMachinePage } from '../../../api/production/xsNotice';
    import { spinProcessPage } from '../../../api/technology/process';
    export default {
        components: { ganttChart },
        mixins: [calHeight],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            checkedData: {
                type: Array,
                default() {
                    return [];
                }
            }
        },
        data() {
            return {
                searchParams: {
                    pageNo: 1,
                    pageSize: pageConfig.pageSize,
                    pageSizes: pageConfig.pageSizes,
                    dateFrom: '',
                    dateTo: '',
                    workshopId: null,
                    processId: null,
                    nameOrCode: ''
                },
                processList: [],
                workshopList: [],
                currentTime: '',
                tableLoading: false,
                tableHeaderSwitchType: 'date',
                tableData: [],
                tableHeader: [
                    { key: 'code', name: '编号', align: 'left', fixed: false, width: 90 },
                    { key: 'name', name: '名称', align: 'left', fixed: false, width: 90 },
                    { key: 'machineModelName', name: '机型', align: 'left', fixed: false, width: 100 },
                    { key: 'processName', name: '工序', align: 'left', fixed: false, width: 60 }
                ],
                activeName: '1',
                showDialog: false,
                checkedArr: []
            };
        },
        methods: {
            onCheckChange(arr) {
                this.checkedArr = arr;
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            // 左调日期
            onArrowLeft(command) {
                this.$set(this.searchParams, 'dateFrom', formatDate(Date.parse(formatDate(this.searchParams.dateFrom)) - 24 * 60 * 60 * 1000 * command));
                this.getListRequest();
            },
            // 今天
            onArrowNow() {
                this.$set(this.searchParams, 'dateFrom', formatDate(Date.parse(formatDate(this.currentTime))));
                this.getListRequest();
            },
            // 右调日期
            onArrowRight(command) {
                this.$set(this.searchParams, 'dateTo', formatDate(Date.parse(formatDate(this.searchParams.dateTo)) + 24 * 60 * 60 * 1000 * command));
                this.getListRequest();
            },
            onConfirmBtn() {
                if (this.checkedArr.length) {
                    this.$emit('on-confirm', this.checkedArr);
                } else {
                    tipsBar(this, 'checkbox');
                }
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
            },
            getListRequest() {
                this.tableLoading = true;
                const params = JSON.parse(JSON.stringify(this.searchParams));
                params.startDate = params.dateFrom;
                params.endDate = params.dateTo;
                delete params.dateFrom;
                delete params.dateTo;
                delete params.count;
                return productionPrdMachinePage(params).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.searchParams.count = res.data.count;
                        this.tableData = res.data.res.map((x) => {
                            x.checked = false;
                            x.taskList = x.prdNoticeMachineDateList ? x.prdNoticeMachineDateList.map(x => {
                                x.productName = x.prdNotice.productName;
                                x.code = x.prdNotice.code;
                                return x;
                            }) : [];
                            this.checkedData.forEach(k => {
                                if (x.id === k.machineId) {
                                    x.checked = true;
                                }
                            });
                            return x;
                        });
                    }
                });
            },
            getCurrentTime() {
                this.currentTime = Date.now();
                const d = new Date();
                let year = d.getFullYear();
                let mm = d.getMonth() + 1;
                for (let i = 0; i < 2; i++) {
                    if (mm === 12) {
                        year += 1;
                        mm = 1;
                    } else {
                        mm++;
                    }
                }
                const mon = mm < 10 ? `0${mm}` : mm;
                let day = d.getDate() < 10 ? `0${d.getDate()}` : d.getDate();
                const lastDay = new Date(year, mon, 0).getDate();
                if (day > lastDay) {
                    day = lastDay;
                }
                const dateStr = `${year}-${mon}-${day}`;
                this.$set(this.searchParams, 'dateFrom', formatDate(Date.now()));
                this.$set(this.searchParams, 'dateTo', dateStr);
            },
            getProcessListRequest() {
                return spinProcessPage().then(res => {
                    if (res.data.status === 200) {
                        this.processList = res.data.res;
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                this.getCurrentTime();
                await this.getProcessListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            }
        },
        mounted() {
            this.getDependentData();
        }
    };
</script>
